package com.yzh.booksystem.controller;

import com.yzh.booksystem.dao.UserDao;
import com.yzh.booksystem.entity.Book;
import com.yzh.booksystem.entity.User;
import com.yzh.booksystem.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ClientInfoStatus;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "LoginServlet",urlPatterns = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");

        String name = request.getParameter("username");
        String password = request.getParameter("password");

        User user = new User();
        user.setName(name);//尤其注意，本身name就是String类型，设置的时候不需要再加“”双引号
        user.setPassword(password);
        UserService userService = new UserService();
        HttpSession session = request.getSession();
        if (name == "" || password == "") {
            response.sendRedirect("login.jsp?error0=no");//如果return的话,后面还有response,
            return;                                                // 如果为空的时候此处已经提交响应了,逻辑上后面不可再次调用response.sendridect()
        }                                                   //但是不为空,后面的就可以正常调用,如果想为空的时候使用,要多加一个return;直接结束,否则不行,好像会是提交响应后还是会检测后面.
//            return;
        //}  //不能直接用上述这种方式判断，显示响应已经被提交
//            if (user.getName() != "" && user.getPassword() != "") {
                //处理管理员登陆（设计一）的流程控制
                try {
                    if (userService.loginUser(user)) {
                        //request.setAttribute("user", user);
                        //request.getRequestDispatcher("/index.jsp").forward(request, response);
                        //处理管理员登陆后(设计二）的流程控制,管理员登陆显示所有数据
                       // try {
                            //if (userService.total_books_show() != null) {
                                //因为index页面要显示所有数据，所以定义变量接收service层的所有数据
                                List<Book> listBooks = userService.total_books_show();
//                                request.setAttribute("listBooks", listBooks);
                                session.setAttribute("listBooks", listBooks);
                                response.sendRedirect("/booksystem/index.jsp");
//                                request.getRequestDispatcher("/index.jsp").forward(request, response);
                           // }
                    } else {
                        response.sendRedirect("login.jsp?error1=yes");
                    /*request.setAttribute("error1", "用户不存在，请重新输入");
                    String error = (String) request.getAttribute("error1");
                    PrintWriter writer = response.getWriter();
                    writer.println(error);*/
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
//            } else {
//                response.sendRedirect("login.jsp?error0=no");
//            }
//

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
